package com.xiaoyi.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.avin.yi.R;
import com.bumptech.glide.Glide;
import com.gyf.barlibrary.ImmersionBar;
import com.xiaoyi.entity.XyData;
import com.xiaoyi.entity.XyDataQueryResult;
import com.xiaoyi.tool.AvBaseTool;
import com.xiaoyi.tool.SharePreferenceUtil;
import com.xiaoyi.tool.UrlTool;
import com.xiaoyi.tool.http.HostConfig;
import com.xiaoyi.tool.http.XyApi;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;


public class SplashActivity extends BaseActivity      {

    private ImageView picShow ;
    private View settingCommit ;
    private Context context ;
    private SharePreferenceUtil sharePreferenceUtil ;
    private EditText hostIp ;
    private final String NAME_KEY ="IP_SAVE_TAG" ;
    private final String IP_KEY  ="HOST_IP" ;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        ImmersionBar.with(this).init();
        if(getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }

        sharePreferenceUtil = new SharePreferenceUtil(this ,NAME_KEY ) ;
        context = this ;
        picShow = findViewById(R.id.picShow);
        hostIp=findViewById(R.id.hostIp) ;
        settingCommit = findViewById(R.id.settingCommit) ;

        findViewById(R.id.settingShow).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hostIp.setVisibility(View.VISIBLE);
                settingCommit.setVisibility(View.VISIBLE);
            }
        });


        settingCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sharePreferenceUtil.saveSharedPreferences(IP_KEY , hostIp.getText().toString()) ;
                AvBaseTool.showMsg(context ,"设置成功! "+ hostIp.getText().toString());
            }
        });


        HostConfig.HOST_IP=sharePreferenceUtil.getStringByKey(IP_KEY) ;
        HostConfig. PROGRAM_IP =  HostConfig.HOST_IP+"AivinInfo/" ;
        hostIp.setText( HostConfig.HOST_IP);
        loadData("启动页");

    }



    private void loadData( final String mType ) {

        showLoadDialog2(context);
        XyApi.getInstance(context).getDataDirByType(new SingleObserver<ArrayList<String>>() {
            @Override
            public void onSubscribe(Disposable d) {    }

            @Override
            public void onSuccess(ArrayList<String> list) {
                loadData2(mType  , list.get( getRadomInt(0 , list.size()-1) )  ) ;
                hideLoadDialog2();

            }

            @Override
            public void onError(Throwable e) {
                AvBaseTool.showMsg(context ,"错误："+ e.getMessage());
                hideLoadDialog2();
            }
        } ,mType  );

    }


    private void loadData2( String mType  ,String mDir ) {
        showLoadDialog2(context);
        XyApi.getInstance(context).getDataByTypeAndDir( new SingleObserver<XyDataQueryResult>() {
            @Override
            public void onSubscribe(Disposable d) {    }

            @Override
            public void onSuccess(XyDataQueryResult  result) {
                showSplashInfo(result.getList() );
                hideLoadDialog2();

            }

            @Override
            public void onError(Throwable e) {
                AvBaseTool.showMsg(context ,"错误："+ e.getMessage());
                hideLoadDialog2();
            }
        } ,mType ,mDir  , 1000 , 1 ,false );

    }

    private void showSplashInfo( List<XyData> list ){
        if(list==null || list.size()==0){
            return;
        }

        XyData xyData = list.get( getRadomInt(0 , list.size()-1) )  ;

        String url = UrlTool.getDataLastUrl(xyData ) ;

        AvBaseTool.e("url===="+ url);
        Glide.with(context).load(  url )  .into(picShow);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                    MainActivity.startActivity((Activity)context);
            }
        } ,3000);
    }


    private int getRadomInt(int startNum, int endNum){
        return new Random().nextInt(endNum - startNum + 1) + startNum;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        context = null ;
        ImmersionBar.with(this).destroy();
    }



}
